using Assets.Scripts.GDK;
using UnityEngine;
using UnityEngine.UI;

namespace Brick
{
    public class UseDaoju : gdk_base_panel
    {
        public Button btnDiamond;

        public GameObject btnAd;

        public override void OnStart()
        {

            //小于200不给复活
            if (GameData.coin < 200)
            {
                btnDiamond.interactable = false;
                btnDiamond.transform.GetChild(0).GetComponent<Text>().text = "点击复活";
            }
        }

        /// <summary>
        /// 用户点击函数
        /// </summary>
        public void OnUseClick()
        {
            gdk_game_manager.PlaySound(AudioID.click);
            if (GameData.coin >= 200)
            {
                GameManager.addCoin(-200);
                gdk_game_manager.DispachEvent(EventID.FUHUO_BOMB3);
                GDK.ModuleManger.Module<LevelInfoBoxModule>().sendUseToolCustomEvent(ToolsID.TOOL_ELIMATE3LINE);
                close();
            }
            else
            {
                gdk_game_manager.showPanel(PanelID.DIAMONDSHOP);
            }
        }

        public void OnAdClick()
        {
            Debug.Log("OnAdClick:������Ƶ���--����");
            gdk_game_manager.PlaySound(AudioID.click);
            AdManager.Instance.ShowRewardedVideoAd(OnAdWatched, OnAdError);
                //Debug.Log("VideoCallBack:��Ƶ���ųɹ�");
                //gdk_game_manager.DispachEvent(EventID.FUHUO_BOMB3);
                //GDK.ModuleManger.Module<LevelInfoBoxModule>().sendUseToolCustomEvent(ToolsID.TOOL_ELIMATE3LINE);
                //close();
           


        }
 
        public void OnUnUseClick()
        {
            gdk_game_manager.PlaySound(AudioID.click);
            gdk_game_manager.showPanel(PanelID.GAMEEND);
            close();
        }

        private void OnAdWatched()
        {
            Debug.Log("VideoCallBack:��Ƶ���ųɹ�");
            gdk_game_manager.DispachEvent(EventID.FUHUO_BOMB3);
            GDK.ModuleManger.Module<LevelInfoBoxModule>().sendUseToolCustomEvent(ToolsID.TOOL_ELIMATE3LINE);
            close();
            Debug.Log("广告观看完成");
        }

        private void OnAdError(string errorMessage)
        {
            Debug.LogError($"广告播放失败: {errorMessage}");
            // 加载 AdError 预制体
            GameObject adErrorPrefab = Resources.Load<GameObject>("AdError");
            if (adErrorPrefab != null)
            {
                // 实例化 AdError
                GameObject adErrorInstance = Instantiate(adErrorPrefab, transform.position, Quaternion.identity);
                // 1 秒后销毁实例
                Destroy(adErrorInstance, 1.5f);
            }
            else
            {
                Debug.LogError("AdError 预制体未找到，请确保其位于 Resources 文件夹中。");
            }
        }
    }
}

